package com.platform.common.utils;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

/**
 * <pre>
 * 功能：转换编码
 * </pre>
 * 
 * @author wei.gao
 */
public class ConvertUtils extends org.apache.commons.beanutils.ConvertUtils {
	/**
	 * 8 位 UCS 转换格式
	 */
	private static final String UTF_8 = "UTF-8";
	
	/**
	 * <pre>
	 * 功能：字符串编码转换的实现方法
	 * </pre>
	 * 
	 * @param strIn
	 * @param sourceCharset
	 * @param targetCharset
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String convertCharset(String strIn, String sourceCharset, String targetCharset) throws UnsupportedEncodingException {
		if (ValidateUtils.isNull(strIn)) {
			return strIn;
		}
		byte[] c = strIn.getBytes(sourceCharset);
		String strOut = new String(c, targetCharset);
		return strOut;
	}
	
	/**
	 * <pre>
	 * 功能：获得UTF8编码的字符串
	 * </pre>
	 * 
	 * @param gbkStr
	 * @return
	 */
	public static String convertGbkToUft8(String gbkStr) {
		try {
			return new String(convertGbkToUtf8Bytes(gbkStr), UTF_8);
		} catch (UnsupportedEncodingException e) {
			throw new InternalError();
		}
	}
	
	/**
	 * <pre>
	 * 功能：获得UTF8编码的字节数组
	 * </pre>
	 * 
	 * @param gbkStr
	 * @return
	 */
	public static byte[] convertGbkToUtf8Bytes(String gbkStr) {
		int n = gbkStr.length();
		byte[] utfBytes = new byte[3 * n];
		int k = 0;
		for (int i = 0; i < n; i++) {
			int m = gbkStr.charAt(i);
			if (m < 128 && m >= 0) {
				utfBytes[k++] = (byte) m;
				continue;
			}
			utfBytes[k++] = (byte) (0xe0 | (m >> 12));
			utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
			utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
		}
		if (k < utfBytes.length) {
			byte[] tmp = new byte[k];
			System.arraycopy(utfBytes, 0, tmp, 0, k);
			return tmp;
		}
		return utfBytes;
	}
	
	/**
	 * <pre>
	 * 功能：半角转换成全角
	 * </pre>
	 * 
	 * @param input
	 * @return
	 */
	public static String convertSBC(String input) {
		// 半角转全角
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 32) {
				c[i] = (char) 12288;
				continue;
			}
			if (c[i] < 127) {
				c[i] = (char) (c[i] + 65248);
			}
		}
		return new String(c);
	}
	
	/**
	 * <pre>
	 * 功能：全角转换成半角
	 * </pre>
	 * 
	 * @param input
	 * @return
	 */
	public static String convertDBC(String input) {
		// 全角转 半角
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375) {
				c[i] = (char) (c[i] - 65248);
			}
		}
		return new String(c);
	}
	
	/**
	 * <pre>
	 * 功能：Hex编码.
	 * </pre>
	 * 
	 * @param input
	 * @return
	 */
	public static String convertEncodeHex(byte[] input) {
		return Hex.encodeHexString(input);
	}
	
	/**
	 * <pre>
	 * 功能：Hex解码.
	 * </pre>
	 * 
	 * @param input
	 * @return
	 */
	public static byte[] convertDecodeHex(String input) {
		try {
			return Hex.decodeHex(input.toCharArray());
		} catch (DecoderException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * <pre>
	 * 功能：Base64编码.
	 * </pre>
	 * 
	 * @param input
	 * @return
	 */
	public static String convertEncodeBase64(byte[] input) {
		return Base64.encodeBase64String(input);
	}
	
	/**
	 * <pre>
	 * 功能：Base64编码, URL安全(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548).
	 * </pre>
	 * 
	 * @param input
	 * @return
	 */
	public static String convertEncodeUrlSafeBase64(byte[] input) {
		return Base64.encodeBase64URLSafeString(input);
	}
	
	/**
	 * <pre>
	 * 功能：Base64解码.
	 * </pre>
	 * 
	 * @param input
	 * @return
	 */
	public static byte[] convertDecodeBase64(String input) {
		return Base64.decodeBase64(input);
	}
}
